package org.miyuki4j.cfgtool;

import java.util.Objects;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 10:47
 * @Version 1.0
 */
public class TableField implements TypeParseable {

    private final int index;
    private final String comment;
    private final String name;
    private final String type;
    private final String delimiters;

    private final String upperName;

    private Type javaType;

    public TableField(int index, String comment, String name, String type, String delimiters) {
        this.index = index;
        this.comment = comment;
        this.name = name;
        this.type = type;
        this.delimiters = delimiters;
        this.upperName = name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public int getIndex() {
        return index;
    }

    public String getComment() {
        return comment;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getDelimiters() {
        return delimiters;
    }

    public String getUpperName() {
        return upperName;
    }

    public Type getJavaType() {
        return javaType;
    }

    public String getDeclareType() {
        return javaType.javaDeclaration();
    }

    public String getDefaultValue() {
        return javaType.defaultValue();
    }

    public String getJavaInterface() {
        return javaType.javaInterface();
    }

    public String getJavaImplementation() {
        return javaType.javaImplementation();
    }

    @Override
    public String toString() {
        return "TableField{" + "index=" + index + ", comment='" + comment + '\'' + ", name='" + name + '\'' + ", type='" + type + '\'' + ", delimiters='" + delimiters + '\'' + ", upperName='" + upperName + '\'' + ", javaType=" + javaType + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        TableField that = (TableField) o;
        return index == that.index && Objects.equals(comment, that.comment) && Objects.equals(name, that.name) && Objects.equals(type, that.type) && Objects.equals(delimiters, that.delimiters);
    }

    @Override
    public int hashCode() {
        return Objects.hash(index, comment, name, type, delimiters);
    }

    @Override
    public void parse() {
        javaType = TypeFactory.parse(this);
    }

    public <T> T instance(String value) {
        return javaType.instance(value, getDelimiters());
    }

    public String verify(String value) {
        return javaType.exportVerify(value, getDelimiters());
    }
}
